﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata.StateTracker;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.Metadata;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core.Const;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.BarElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args.UnitTest;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App.Data;

namespace Sincetech.K3.SCM.Common
{
    /// <summary>
    /// 入库单据：单据插件(更新扩展计量单位对应数量的库存)
    /// 库存更新规则：基本单位数量、长度、支数、双数(不考虑双数明细)中只要存在一个值更新后的库存数量小于0，都不允许更新
    /// </summary>
    public class InStock_Bill : AbstractBillPlugIn
    {
        //DataSet ds = new DataSet();
        string strSql;

        //表头表名、表体表名、双数明细表名
        string strHeadTable, strEntryTable, strPairDetailTable;
        DataTable oldData = new DataTable();
        DataTable newData = new DataTable();
        DataTable oldDataDetail = new DataTable();
        DataTable newDataDetail = new DataTable();
        DataTable invData = new DataTable();
        DataTable invDataDetail = new DataTable();

        List<Dictionary<string, string>> paramL = new List<Dictionary<string, string>>();

        /*

        #region 单据保存后事件
        /// <summary>
        /// 保存后事件
        /// </summary>
        /// <param name="e"></param>
        public override void AfterSave(Kingdee.BOS.Core.Bill.PlugIn.Args.AfterSaveEventArgs e)
        {
            base.AfterSave(e);
            var openType = this.View.OpenParameter.InitStatus;

            #region 获取保存后的基本信息和明细信息
            List<Dictionary<string, string>> paramList = paramL;

            //保存后的明细信息表
            string str_FSTOCKLOCID = "";
            string str_FLOT = "";
            if (string.IsNullOrEmpty(paramList[0]["FSTOCKLOCID"]))
            {
                str_FSTOCKLOCID = " FSTOCKLOCID =0 ";
            }
            else
            {
                str_FSTOCKLOCID = " FSTOCKLOCID=" + paramList[0]["FSTOCKLOCID"] + " ";
            }
            if (string.IsNullOrEmpty(paramList[0]["FLOT"]))
            {
                str_FLOT = " FLOT_TEXT is null ";
            }
            else
            {
                str_FLOT = " FLOT_TEXT='" + paramList[0]["FLOT"] + "' ";
            }

            string strSql = string.Format(@"select * from [dbo].[T_STK_INSTOCKENTRY] t
                left join [dbo].[t_STK_InStock] t1 on t.fid=t1.fid and t1.FSTOCKORGID={0}
                where t.FOWNERID={1} and t.FKEEPERID={2} and t.FSTOCKID={3} and " + str_FSTOCKLOCID + " and " + str_FLOT + " and t.FMATERIALID={4}",
                paramList[0]["FSTOCKORGID"], paramList[0]["FOWNERID"],
                paramList[0]["FKEEPERID"], paramList[0]["FSTOCKID"], paramList[0]["FMATERIALID"]);
            for (int i = 1; i < paramList.Count; i++)
            {
                if (string.IsNullOrEmpty(paramList[i]["FSTOCKLOCID"]))
                {
                    str_FSTOCKLOCID = " FSTOCKLOCID =0 ";
                }
                else
                {
                    str_FSTOCKLOCID = " FSTOCKLOCID=" + paramList[i]["FSTOCKLOCID"] + " ";
                }
                if (string.IsNullOrEmpty(paramList[i]["FLOT"]))
                {
                    str_FLOT = " FLOT_TEXT is null ";
                }
                else
                {
                    str_FLOT = " FLOT_TEXT='" + paramList[i]["FLOT"] + "' ";
                }
                string strSql1 = string.Format(@" union select * from [dbo].[T_STK_INSTOCKENTRY] t 
                    left join [dbo].[t_STK_InStock] t1 on t.fid=t1.fid and t1.FSTOCKORGID={0} 
                    where t.FOWNERID={1} and t.FKEEPERID={2} and t.FSTOCKID={3} and " + str_FSTOCKLOCID + " and " + str_FLOT + " and t.FMATERIALID={4}",
                paramList[i]["FSTOCKORGID"], paramList[i]["FOWNERID"],
                paramList[i]["FKEEPERID"], paramList[i]["FSTOCKID"], paramList[i]["FMATERIALID"]);
                strSql += strSql1;
            }

            DataTable th = new DataTable("second");
            th = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];
            second = th;

            //保存后的双数明细表
            DataTable th1 = new DataTable("second_D");
            int rowNum_D = 0;
            string strSql_D = "";
            if (th.Rows.Count > 0)
            {
                foreach (DataRow datarow in th.Rows)
                {

                    if (rowNum_D == 0)
                    {
                        strSql_D = string.Format(@"SELECT  t2.FID,t.FEntryID,t.F_ST_COLOR,t.F_ST_LR,t.F_ST_PAIRDETAILQTY,t.F_ST_SIZE
                                              FROM    st_t_STK_INSTOCKENTRY_Pair t
                                              LEFT JOIN T_STK_INSTOCKENTRY t1 ON t1.FENTRYID = t.FEntryID 
                                              AND t1.FOWNERID={0}
                                              AND t1.FKEEPERID={1} 
                                              AND t1.FSTOCKID={2}
                                              AND t1.FLOT={3} 
                                              AND t1.FSTOCKLOCID={4}
                                              AND t1.FMATERIALID={5}
                                              LEFT JOIN [T_STK_INVENTORY] t2 ON t2.FOWNERID = t1.FOWNERID
                                              AND t2.FKEEPERID = t1.FKEEPERID
                                              AND t2.FSTOCKID = t1.FSTOCKID
                                              AND t2.FLOT = t1.FLOT
                                              AND t2.FSTOCKLOCID = t1.FSTOCKLOCID
                                              AND t2.FMATERIALID = t1.FMATERIALID 
                                              where t.FEntryID={6} ", datarow["FOWNERID"], datarow["FKEEPERID"], datarow["FSTOCKID"], datarow["FLOT"], datarow["FSTOCKLOCID"], datarow["FMATERIALID"], datarow["FENTRYID"]);
                    }
                    else
                    {
                        string strSql_D1 = string.Format(@" union SELECT  t2.FID,t.FEntryID,t.F_ST_COLOR,t.F_ST_LR,t.F_ST_PAIRDETAILQTY,t.F_ST_SIZE
                                              FROM    st_t_STK_INSTOCKENTRY_Pair t
                                              LEFT JOIN T_STK_INSTOCKENTRY t1 ON t1.FENTRYID = t.FEntryID 
                                              AND t1.FOWNERID={0}
                                              AND t1.FKEEPERID={1} 
                                              AND t1.FSTOCKID={2}
                                              AND t1.FLOT={3} 
                                              AND t1.FSTOCKLOCID={4}
                                              AND t1.FMATERIALID={5}
                                              LEFT JOIN [T_STK_INVENTORY] t2 ON t2.FOWNERID = t1.FOWNERID
                                              AND t2.FKEEPERID = t1.FKEEPERID
                                              AND t2.FSTOCKID = t1.FSTOCKID
                                              AND t2.FLOT = t1.FLOT
                                              AND t2.FSTOCKLOCID = t1.FSTOCKLOCID
                                              AND t2.FMATERIALID = t1.FMATERIALID 
                                              where t.FEntryID={6}", datarow["FOWNERID"], datarow["FKEEPERID"], datarow["FSTOCKID"], datarow["FLOT"], datarow["FSTOCKLOCID"], datarow["FMATERIALID"], datarow["FENTRYID"]);
                        strSql_D += strSql_D1;
                    }
                    rowNum_D++;
                }

                th1 = DBUtils.ExecuteDataSet(this.Context, strSql_D).Tables[0];
                second_D = th1;
            }
            #endregion

            #region 关联原即时库存表，更新扩展后的即时库存表

            #region 新增和累加
            if (first.Rows.Count == 0)
            {
                if (second.Rows.Count > 0)
                {
                    //新增和累加,通过用保存后的入库和原库存数据来查询扩展库存是否有该物料的数据，有则累加，无则新增
                    for (int i = 0; i < second.Rows.Count; i++)
                    {
                        string str_EXP = string.Format(@"select * from st_T_STK_INVENTORY_EXP t where t.F_ST_SALEORDERBILLNO={0} 
                                                                    and t.F_ST_PURORDERBILLNO={1} 
                                                                    and t.F_ST_SPROWNO={2} and t.F_ST_SUPPLYID={3}
                                                                    and t.F_ST_ORIGINALBATCH={4} 
                                                                    ", second.Rows[i]["F_ST_SALEORDERBILLNO"], second.Rows[i]["F_ST_PURORDERBILLNO"],
                                                                     second.Rows[i]["F_ST_SPROWNO"], second.Rows[i]["F_ST_SUPPLYID"],
                                                                         second.Rows[i]["F_ST_ORIGINALBATCH"]);

                        DataTable valid_Exp_Ora = DBUtils.ExecuteDataSet(this.Context, str_EXP).Tables[0];
                        //累加
                        if (valid_Exp_Ora.Rows.Count>0)
                        {
                            #region 扩展库存双数累加

                            //原扩展库存的
                            //DataRow[] drs = valid_Exp_Ora.Select("F_ST_SALEORDERBILLNO=" + second.Rows[i]["F_ST_SALEORDERBILLNO"] + " and  F_ST_PURORDERBILLNO=" + second.Rows[i]["F_ST_PURORDERBILLNO"] +
                            //" and F_ST_SPROWNO=" + second.Rows[i]["F_ST_SPROWNO"] + " and F_ST_SUPPLYID=" + second.Rows[i]["F_ST_SUPPLYID"] + " and F_ST_ORIGINALBATCH=" + second.Rows[i]["F_ST_ORIGINALBATCH"] + "");
                            //修改后
                            DataRow[] drs = second.Select("F_ST_SALEORDERBILLNO=" + second.Rows[i]["F_ST_SALEORDERBILLNO"] + " and  F_ST_PURORDERBILLNO=" + second.Rows[i]["F_ST_PURORDERBILLNO"] +
                            " and F_ST_SPROWNO=" + second.Rows[i]["F_ST_SPROWNO"] + " and F_ST_SUPPLYID=" + second.Rows[i]["F_ST_SUPPLYID"] + " and F_ST_ORIGINALBATCH=" + second.Rows[i]["F_ST_ORIGINALBATCH"] + "");
                            //累加的双数
                            decimal newPairqty = Convert.ToDecimal(valid_Exp_Ora.Rows[0]["F_ST_PAIRQTY"].ToString()) + Convert.ToDecimal(drs[0]["F_ST_PAIRQTY"].ToString());
                            string str_INVENTORY_EXP_UPDATE = string.Format(@"UPDATE st_T_STK_INVENTORY_EXP  SET F_ST_PAIRQTY={0} where F_ST_SALEORDERBILLNO={1} 
                                                                    and F_ST_PURORDERBILLNO={2} 
                                                                    and F_ST_SPROWNO={3} and F_ST_SUPPLYID={4}
                                                                    and F_ST_ORIGINALBATCH={5} 
                                                                    ", newPairqty, second.Rows[i]["F_ST_SALEORDERBILLNO"], second.Rows[i]["F_ST_PURORDERBILLNO"],
                                                                 second.Rows[i]["F_ST_SPROWNO"], second.Rows[i]["F_ST_SUPPLYID"],
                                                                 second.Rows[i]["F_ST_ORIGINALBATCH"]);
                            int isUpdate = DBUtils.Execute(this.Context, str_INVENTORY_EXP_UPDATE);

                            #endregion

                            #region 扩展库存双数明细累加
                            //获取扩展库存的fid关联的双数明细
                            string str_INVENTORY_EXP_PAIR_UPDATE = string.Format(@"select * from st_T_STK_INVENTORY_EXPENTRY t where t.FID='{0}'", valid_Exp_Ora.Rows[0]["FID"]);
                            DataTable valid_Exp_Pair_Ora = DBUtils.ExecuteDataSet(this.Context, str_INVENTORY_EXP_PAIR_UPDATE).Tables[0];


                            //循环同一个fid的所有双数明细
                            foreach (DataRow dr in valid_Exp_Pair_Ora.Rows)
                            {
                                //获取保存后的入库的双数明细
                                drs = second_D.Select(" F_ST_SIZE='" + dr["F_ST_SIZE"] + "' AND FID = '" + dr["FID"] + "'");

                                decimal str_F_ST_PAIRDETAILQTY = Convert.ToDecimal(dr["F_ST_PAIRDETAILQTY"].ToString()) + Convert.ToDecimal(drs[0]["F_ST_PAIRDETAILQTY"].ToString());

                                string str_INVENTORY_EXP_UPDATE_PAIR = string.Format(@"UPDATE st_T_STK_INVENTORY_EXPENTRY  SET F_ST_PAIRDETAILQTY={0} where FID='{1}' AND F_ST_SIZE='{2}'  ", str_F_ST_PAIRDETAILQTY, dr["FID"], dr["F_ST_SIZE"]);
                                int isUpdate2 = DBUtils.Execute(this.Context, str_INVENTORY_EXP_UPDATE_PAIR);
                            }
                            #endregion

                        }
                        //新增
                        else
                        {
                            string str_FLOT_EXP = "";
                            if (second.Rows[i]["FLOT_TEXT"] == null)
                            {
                                str_FLOT_EXP = " FLOT_TEXT is null ";
                            }
                            else
                            {
                                str_FLOT_EXP = " FLOT_TEXT='" + second.Rows[i]["FLOT_TEXT"].ToString() + "' ";
                            }
                            //通过second表和second_D表来关联库存，并更新扩展库存
                            string strExp = string.Format(@"select t1.FID, 
                                                t.F_ST_SALEORDERBILLNO,
                                                t.F_ST_PURORDERBILLNO,
                                                t.F_ST_SPROWNO,
                                                t.F_ST_SUPPLYID,
                                                t.F_ST_ORIGINALBATCH,
                                                t.F_ST_QTY,
                                                t.F_ST_COUNTQTY,
                                                t.F_ST_PAIRQTY,
                                                t2.F_ST_COLOR,
                                                t2.F_ST_LR,
                                                t2.F_ST_SIZE,
                                                t2.F_ST_PAIRDETAILQTY
                                                from [dbo].[T_STK_INSTOCKENTRY] t left join [dbo].[T_STK_INVENTORY] t1 on t.FOWNERID=t1.FOWNERID 
                                                and t.FKEEPERID=t1.FKEEPERID and t.FSTOCKID=t1.FSTOCKID and  t.FSTOCKLOCID=t1.FSTOCKLOCID and t.FLOT=t1.FLOT
                                                and t.FMATERIALID=t1.FMATERIALID left join st_t_STK_INSTOCKENTRY_Pair t2 on t.FENTRYID=t2.FEntryID 
                                                where  t.FOWNERID={0} and t.FKEEPERID={1} and t.FSTOCKID={2} and t.FSTOCKLOCID={3}. and " + str_FLOT_EXP + " and t.FMATERIALID={4}",
                                                            second.Rows[i]["FOWNERID"].ToString(), second.Rows[i]["FKEEPERID"].ToString(),
                                                            second.Rows[i]["FSTOCKID"].ToString(), second.Rows[i]["FSTOCKLOCID"].ToString(),
                                                            second.Rows[i]["FMATERIALID"].ToString());
                            DataTable th_Exp = new DataTable();
                            th_Exp = DBUtils.ExecuteDataSet(this.Context, strExp).Tables[0];

                            string strExp_Insert = @"Insert into st_T_STK_INVENTORY_EXP(FID,F_ST_SALEORDERBILLNO,F_ST_PURORDERBILLNO,F_ST_SPROWNO,F_ST_SUPPLYID,F_ST_ORIGINALBATCH,F_ST_QTY,F_ST_COUNTQTY,F_ST_PAIRQTY)
                                                values(@FID,@F_ST_SALEORDERBILLNO,@F_ST_PURORDERBILLNO,@F_ST_SPROWNO,@F_ST_SUPPLYID,@F_ST_ORIGINALBATCH,@F_ST_QTY,@F_ST_COUNTQTY,@F_ST_PAIRQTY)";
                            List<SqlParam> one_List = new List<SqlParam>();
                            one_List.Add(new SqlParam("@FID", KDDbType.String, th_Exp.Rows[0]["FID"]));
                            one_List.Add(new SqlParam("@F_ST_SALEORDERBILLNO", KDDbType.String, th_Exp.Rows[0]["F_ST_SALEORDERBILLNO"]));
                            one_List.Add(new SqlParam("@F_ST_PURORDERBILLNO", KDDbType.String, th_Exp.Rows[0]["F_ST_PURORDERBILLNO"]));
                            one_List.Add(new SqlParam("@F_ST_SPROWNO", KDDbType.Int32, th_Exp.Rows[0]["F_ST_SPROWNO"]));
                            one_List.Add(new SqlParam("@F_ST_SUPPLYID", KDDbType.String, th_Exp.Rows[0]["F_ST_SUPPLYID"]));
                            one_List.Add(new SqlParam("@F_ST_ORIGINALBATCH", KDDbType.String, th_Exp.Rows[0]["F_ST_ORIGINALBATCH"]));
                            one_List.Add(new SqlParam("@F_ST_QTY", KDDbType.Decimal, th_Exp.Rows[0]["F_ST_ORIGINALBATCH"]));
                            one_List.Add(new SqlParam("@F_ST_COUNTQTY", KDDbType.Decimal, th_Exp.Rows[0]["F_ST_COUNTQTY"]));
                            one_List.Add(new SqlParam("@F_ST_PAIRQTY", KDDbType.Decimal, th_Exp.Rows[0]["F_ST_PAIRQTY"]));
                            int isInsert = DBUtils.Execute(this.Context, strExp_Insert, one_List);
                            if (isInsert > 0)
                            {
                                foreach (DataRow dr in th_Exp.Rows)
                                {
                                    string strExpPair_Insert = @"Insert into st_T_STK_INVENTORY_EXPENTRY(FID,F_ST_COLOR,F_ST_LR,F_ST_SIZE,F_ST_PAIRDETAILQTY)
                                                            values(@FID,@F_ST_COLOR,@F_ST_LR,@F_ST_SIZE,@F_ST_PAIRDETAILQTY)";
                                    List<SqlParam> one_List_S = new List<SqlParam>();
                                    one_List_S.Add(new SqlParam("@FID", KDDbType.String, dr["FID"]));
                                    one_List_S.Add(new SqlParam("@F_ST_COLOR", KDDbType.String, dr["F_ST_COLOR"]));
                                    one_List_S.Add(new SqlParam("@F_ST_LR", KDDbType.String, dr["F_ST_LR"]));
                                    one_List_S.Add(new SqlParam("@F_ST_SIZE", KDDbType.String, dr["F_ST_SIZE"]));
                                    one_List_S.Add(new SqlParam("@F_ST_PAIRDETAILQTY", KDDbType.Decimal, dr["F_ST_PAIRDETAILQTY"]));
                                    int isInsert1 = DBUtils.Execute(this.Context, strExpPair_Insert, one_List_S);
                                }
                            }
                        }
                    }
                }
            }


            #endregion

            #region 更新

            if (first.Rows.Count > 0)
            {
                if (second.Rows.Count > 0)
                {
                    for (int i = 0; i < second.Rows.Count; i++)
                    {
                        #region 获取扩展的库存数据
                        string str_INVENTORY_EXP = string.Format(@"select * from st_T_STK_INVENTORY_EXP t where t.F_ST_SALEORDERBILLNO={0} 
                                                                    and t.F_ST_PURORDERBILLNO={1} 
                                                                    and t.F_ST_SPROWNO={2} and t.F_ST_SUPPLYID={3}
                                                                    and t.F_ST_ORIGINALBATCH={4} 
                                                                    ", second.Rows[i]["F_ST_SALEORDERBILLNO"], second.Rows[i]["F_ST_PURORDERBILLNO"],
                                                                     second.Rows[i]["F_ST_SPROWNO"], second.Rows[i]["F_ST_SUPPLYID"],
                                                                         second.Rows[i]["F_ST_ORIGINALBATCH"]
                                                                     );
                        DataTable th_Exp_Ora = DBUtils.ExecuteDataSet(this.Context, str_INVENTORY_EXP).Tables[0];
                        if (th_Exp_Ora != null)
                        {
                            #region 更新双数

                            //修改前
                            DataRow[] drs = first.Select("F_ST_SALEORDERBILLNO=" + second.Rows[i]["F_ST_SALEORDERBILLNO"] + " and  F_ST_PURORDERBILLNO=" + second.Rows[i]["F_ST_PURORDERBILLNO"] +
                            " and F_ST_SPROWNO=" + second.Rows[i]["F_ST_SPROWNO"] + " and F_ST_SUPPLYID=" + second.Rows[i]["F_ST_SUPPLYID"] + " and F_ST_ORIGINALBATCH=" + second.Rows[i]["F_ST_ORIGINALBATCH"] + "");

                            //修改后
                            DataRow[] drs1 = second.Select("F_ST_SALEORDERBILLNO=" + second.Rows[i]["F_ST_SALEORDERBILLNO"] + " and  F_ST_PURORDERBILLNO=" + second.Rows[i]["F_ST_PURORDERBILLNO"] +
                            " and F_ST_SPROWNO=" + second.Rows[i]["F_ST_SPROWNO"] + " and F_ST_SUPPLYID=" + second.Rows[i]["F_ST_SUPPLYID"] + " and F_ST_ORIGINALBATCH=" + second.Rows[i]["F_ST_ORIGINALBATCH"] + "");

                            //扩展的库存双数-修改前+修改后
                            decimal newPairqty = Convert.ToDecimal(th_Exp_Ora.Rows[0]["F_ST_PAIRQTY"].ToString()) - Convert.ToDecimal(drs[0]["F_ST_PAIRQTY"].ToString()) + Convert.ToDecimal(drs1[0]["F_ST_PAIRQTY"].ToString());

                            string str_INVENTORY_EXP_UPDATE = string.Format(@"UPDATE st_T_STK_INVENTORY_EXP  SET F_ST_PAIRQTY={0} where F_ST_SALEORDERBILLNO={1} 
                                                                    and F_ST_PURORDERBILLNO={2} 
                                                                    and F_ST_SPROWNO={3} and F_ST_SUPPLYID={4}
                                                                    and F_ST_ORIGINALBATCH={5} 
                                                                    ", newPairqty, second.Rows[i]["F_ST_SALEORDERBILLNO"], second.Rows[i]["F_ST_PURORDERBILLNO"],
                                                                     second.Rows[i]["F_ST_SPROWNO"], second.Rows[i]["F_ST_SUPPLYID"],
                                                                     second.Rows[i]["F_ST_ORIGINALBATCH"]);

                            int isInsert2 = DBUtils.Execute(this.Context, str_INVENTORY_EXP_UPDATE);

                            #endregion

                            #region 更新双数明细
                            //获取扩展库存的fid关联的双数明细
                            string str_INVENTORY_EXP_PAIR = string.Format(@"select * from st_T_STK_INVENTORY_EXPENTRY t where t.FID='{0}'", th_Exp_Ora.Rows[0]["FID"]);
                            DataTable th_Exp_Pair_Ora = DBUtils.ExecuteDataSet(this.Context, str_INVENTORY_EXP_PAIR).Tables[0];


                            //循环同一个fid的所有双数明细
                            foreach (DataRow dr in th_Exp_Pair_Ora.Rows)
                            {
                                //获取保存后的入库的双数明细
                                drs = second_D.Select(" F_ST_SIZE='" + dr["F_ST_SIZE"] + "' AND FID = '" + dr["FID"] + "'");
                                drs1 = first_D.Select(" FEntryID=" + drs[0]["FEntryID"] + " AND F_ST_SIZE='" + dr["F_ST_SIZE"] + "' ");

                                decimal str_F_ST_PAIRDETAILQTY = Convert.ToDecimal(dr["F_ST_PAIRDETAILQTY"].ToString()) + Convert.ToDecimal(drs[0]["F_ST_PAIRDETAILQTY"].ToString()) - Convert.ToDecimal(drs1[0]["F_ST_PAIRDETAILQTY"].ToString());

                                string str_INVENTORY_EXP_UPDATE_PAIR = string.Format(@"UPDATE st_T_STK_INVENTORY_EXPENTRY  SET F_ST_PAIRDETAILQTY={0} where FID='{1}' AND F_ST_SIZE='{2}' ", str_F_ST_PAIRDETAILQTY, dr["FID"], dr["F_ST_SIZE"]);
                                int isUpdate2 = DBUtils.Execute(this.Context, str_INVENTORY_EXP_UPDATE_PAIR);
                            }
                            #endregion
                        }
                        #endregion
                    }
                }
            }

            #endregion

            #endregion

        }
        #endregion

        */

        #region 单据保存前事件
        /// <summary>
        /// 保存前事件
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeSave(Kingdee.BOS.Core.Bill.PlugIn.Args.BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);

            //设置表名
            strHeadTable = "T_STK_InStock";
            strEntryTable  = "T_STK_InStockEntry";
            strPairDetailTable = "st_t_STK_InStockEntry_Pair";

            var openType = this.View.OpenParameter.InitStatus;
            //单据保存前，判断双数明细的总数量是否与对应对应的主单据体的双数一致(避免双数明细录入后，手工修改父单据体的双数)
            /*
             * 注意SubEntryEntity与EntryEntity的区别
             */
            //获取子单据体对象
            SubEntryEntity subEntity = this.View.BusinessInfo.GetEntity("FSubEntity") as SubEntryEntity;
            
            //根据子单据体，获取对应的父单据体
            EntryEntity Entity = subEntity.ParentEntity;

            //获取父单据体的动态对象集合
            DynamicObjectCollection entryRows = this.View.Model.GetEntityDataObject(Entity);

            //获取子单据体
            EntryEntity SubEntity = this.View.BusinessInfo.GetEntryEntity("FSubEntity");

            int rowNum = 1;
            // TODO : subEntryRows为每条主单据体行下所挂的子单据体行集合；
            // TODO : subEntryRows[0] 为第一条子单据体行
            
            //明细信息中的所有数据
            List<Dictionary<string, string>> paramList = new List<Dictionary<string, string>>();
            //双数明细中的所有数据
            List<Dictionary<string, string>> pairDetailList = new List<Dictionary<string, string>>();

            foreach (var entryRow in entryRows)
            {
                rowNum++;
                
                //判断父单据体每行的双数是否与对应的子单据体的双数明细合计一致
                DynamicObjectCollection subEntryRows = SubEntity.DynamicProperty.GetValue(entryRow) as DynamicObjectCollection;
                int subTotal = 0;
                foreach (var subEntryRow in subEntryRows)
                {
                    subTotal += Convert.ToInt32(subEntryRow["F_ST_PAIRDETAILQTY"]);

                    #region  双数明细数据
                    //将双数明细中每一行的数据取出，放入键值对子集合中
                    Dictionary<string, string> pairDetail = new Dictionary<string, string>();

                    //货主类型
                    DynamicObject OWNERTYPEID_PAIR = entryRow["OWNERTYPEID"] as DynamicObject;
                    if (OWNERTYPEID_PAIR == null)
                    {
                        pairDetail.Add("OWNERTYPEID", "");
                    }
                    else
                    {
                        pairDetail.Add("OWNERTYPEID", OWNERTYPEID_PAIR["id"].ToString());
                    }

                    //货主
                    DynamicObject OWNERID_PAIR = entryRow["OWNERID"] as DynamicObject;
                    if (OWNERID_PAIR == null)
                    {
                        pairDetail.Add("OWNERID", "0");
                    }
                    else
                    {
                        pairDetail.Add("OWNERID", OWNERID_PAIR["Id"].ToString());
                    }

                    //保管者类型
                    DynamicObject KEEPERTYPEID_PAIR = entryRow["KEEPERTYPEID"] as DynamicObject;
                    if (KEEPERTYPEID_PAIR == null)
                    {
                        pairDetail.Add("KEEPERTYPEID", "");
                    }
                    else
                    {
                        pairDetail.Add("KEEPERTYPEID", KEEPERTYPEID_PAIR["Id"].ToString());
                    }

                    //保管者
                    DynamicObject KeeperID_PAIR = entryRow["KeeperID"] as DynamicObject;
                    if (KeeperID_PAIR == null)
                    {
                        pairDetail.Add("KEEPERID", "0");
                    }
                    else
                    {
                        pairDetail.Add("KEEPERID", KeeperID_PAIR["Id"].ToString());
                    }

                    //库存组织
                    DynamicObject STOCKORGID_PAIR = this.View.Model.GetValue("FSTOCKORGID") as DynamicObject;
                    if (STOCKORGID_PAIR == null)
                    {
                        pairDetail.Add("STOCKORGID", "0");
                    }
                    else
                    {
                        pairDetail.Add("STOCKORGID", STOCKORGID_PAIR["Id"].ToString());
                    }

                    //仓库内码
                    DynamicObject StockId_PAIR = entryRow["StockId"] as DynamicObject;
                    if (StockId_PAIR == null)
                    {
                        pairDetail.Add("STOCKID", "0");
                    }
                    else
                    {
                        pairDetail.Add("STOCKID", StockId_PAIR["Id"].ToString());
                    }

                    //仓位内码
                    DynamicObject StockLocId_PAIR = entryRow["StockLocId"] as DynamicObject;
                    if (StockLocId_PAIR == null)
                    {
                        pairDetail.Add("STOCKLOCID", "0");
                    }
                    else
                    {
                        pairDetail.Add("STOCKLOCID", StockLocId_PAIR["Id"].ToString());
                    }

                    //辅助属性
                    DynamicObject AUXPROPID_PAIR = entryRow["AuxPropId"] as DynamicObject;
                    if (AUXPROPID_PAIR == null)
                    {
                        pairDetail.Add("AUXPROPID", "0");
                    }
                    else
                    {
                        pairDetail.Add("AUXPROPID", AUXPROPID_PAIR["Id"].ToString());
                    }

                    //库存状态
                    DynamicObject STOCKSTATUSID_PAIR = entryRow["StockStatusId"] as DynamicObject;
                    if (STOCKSTATUSID_PAIR == null)
                    {
                        pairDetail.Add("STOCKSTATUSID", "0");
                    }
                    else
                    {
                        pairDetail.Add("STOCKSTATUSID", STOCKSTATUSID_PAIR["Id"].ToString());
                    }

                    //批号
                    //DynamicObject Lot = entryRow["Lot_Text"] as DynamicObject;
                    if (entryRow["Lot_Text"] == null)
                    {
                        pairDetail.Add("LOT", "");
                    }
                    else
                    {
                        pairDetail.Add("LOT", entryRow["Lot_Text"].ToString());
                    }

                    //BOM版本
                    DynamicObject BOMID_PAIR = entryRow["BOMId"] as DynamicObject;
                    if (BOMID_PAIR == null)
                    {
                        pairDetail.Add("BOMID", "0");
                    }
                    else
                    {
                        pairDetail.Add("BOMID", BOMID_PAIR["Id"].ToString());
                    }

                    //计划跟踪号
                    DynamicObject MTONO_PAIR = entryRow["MtoNo"] as DynamicObject;
                    if (MTONO_PAIR == null)
                    {
                        pairDetail.Add("MTONO", "");
                    }
                    else
                    {
                        pairDetail.Add("MTONO", MTONO_PAIR["Id"].ToString());
                    }

                    //项目编号 FPROJECTNO
                    DynamicObject PROJECTNO_PAIR = entryRow["ProjectNo"] as DynamicObject;
                    if (PROJECTNO_PAIR == null)
                    {
                        pairDetail.Add("PROJECTNO", "");
                    }
                    else
                    {
                        pairDetail.Add("PROJECTNO", PROJECTNO_PAIR["Id"].ToString());
                    }

                    //生产日期 FPRODUCEDATE
                    DynamicObject PRODUCEDATE_PAIR = entryRow["ProduceDate"] as DynamicObject;
                    if (PRODUCEDATE_PAIR == null)
                    {
                        pairDetail.Add("PRODUCEDATE", "");
                    }
                    else
                    {
                        pairDetail.Add("PRODUCEDATE", PRODUCEDATE_PAIR["Id"].ToString());
                    }

                    //有效期至 FEXPIRYDATE
                    DynamicObject EXPIRYDATE_PAIR = entryRow["EXPIRYDATE"] as DynamicObject;
                    if (EXPIRYDATE_PAIR == null)
                    {
                        pairDetail.Add("EXPIRYDATE", "");
                    }
                    else
                    {
                        pairDetail.Add("EXPIRYDATE", EXPIRYDATE_PAIR["Id"].ToString());
                    }

                    //物料内码
                    DynamicObject MaterialId_PAIR = entryRow["MaterialId"] as DynamicObject;
                    if (MaterialId_PAIR == null)
                    {
                        pairDetail.Add("MATERIALID", "");
                    }
                    else
                    {
                        pairDetail.Add("MATERIALID", MaterialId_PAIR["Id"].ToString());
                    }
                    //颜色
                    DynamicObject F_ST_COLOR = entryRow["F_st_Color"] as DynamicObject;
                    if (F_ST_COLOR == null)
                    {
                        pairDetail.Add("F_ST_COLOR", "");
                    }
                    else
                    {
                        pairDetail.Add("F_ST_COLOR", F_ST_COLOR["id"].ToString());
                    }
                    //LR
                    DynamicObject F_ST_LR = entryRow["F_st_LR"] as DynamicObject;
                    if (F_ST_LR == null)
                    {
                        pairDetail.Add("F_ST_LR", "");
                    }
                    else
                    {
                        pairDetail.Add("F_ST_LR", F_ST_LR["id"].ToString());
                    }
                    //尺码
                    DynamicObject F_ST_SIZE = entryRow["F_st_Size"] as DynamicObject;
                    if (F_ST_SIZE == null)
                    {
                        pairDetail.Add("F_ST_SIZE", "");
                    }
                    else
                    {
                        pairDetail.Add("F_ST_SIZE", F_ST_SIZE["id"].ToString());
                    }
                    //双数
                    DynamicObject F_ST_PAIRDETAILQTY = entryRow["F_st_PairDetailQty"] as DynamicObject;
                    if (F_ST_PAIRDETAILQTY == null)
                    {
                        pairDetail.Add("F_ST_PAIRDETAILQTY", "0");
                    }
                    else
                    {
                        pairDetail.Add("F_ST_PAIRDETAILQTY", F_ST_PAIRDETAILQTY["id"].ToString());
                    }
                    #endregion
                    pairDetailList.Add(pairDetail);

                }
                if (Convert.ToInt32(entryRow["F_ST_PAIRQTY"]) != subTotal)
                {
                    this.View.ShowMessage("第[" + rowNum + "]行明细信息的双数与对应的双数明细合计不一致，单据保存未成功！", 
                        MessageBoxType.Notice);
                    e.Cancel = true;
                    return;      
                }

                #region 明细信息数据
                //将明细信息中每一行的数据取出，放入键值对子集合中
                Dictionary<string, string> param = new Dictionary<string, string>();

                //货主类型
                DynamicObject OWNERTYPEID = entryRow["OWNERTYPEID"] as DynamicObject;
                if (OWNERTYPEID == null)
                {
                    param.Add("OWNERTYPEID", "");
                }
                else
                {
                    param.Add("OWNERTYPEID", OWNERTYPEID["id"].ToString());
                }

                //货主
                DynamicObject OWNERID = entryRow["OWNERID"] as DynamicObject;
                if (OWNERID == null)
                {
                    param.Add("OWNERID", "0");
                }
                else
                {
                    param.Add("OWNERID", OWNERID["Id"].ToString());
                }

                //保管者类型
                DynamicObject KEEPERTYPEID = entryRow["KEEPERTYPEID"] as DynamicObject;
                if (KEEPERTYPEID == null)
                {
                    param.Add("KEEPERTYPEID", "");
                }
                else
                {
                    param.Add("KEEPERTYPEID", KEEPERTYPEID["Id"].ToString());
                }

                //保管者
                DynamicObject KeeperID = entryRow["KeeperID"] as DynamicObject;
                if (KeeperID == null)
                {
                    param.Add("KEEPERID", "0");
                }
                else
                {
                    param.Add("KEEPERID", KeeperID["Id"].ToString());
                }

                //库存组织
                DynamicObject STOCKORGID = this.View.Model.GetValue("FSTOCKORGID") as DynamicObject;
                if (STOCKORGID == null)
                {
                    param.Add("STOCKORGID", "0");
                }
                else
                {
                    param.Add("STOCKORGID", STOCKORGID["Id"].ToString());
                }

                //仓库内码
                DynamicObject StockId = entryRow["StockId"] as DynamicObject;
                if (StockId == null)
                {
                    param.Add("STOCKID", "0");
                }
                else
                {
                    param.Add("STOCKID", StockId["Id"].ToString());
                }

                //仓位内码
                DynamicObject StockLocId = entryRow["StockLocId"] as DynamicObject;
                if (StockLocId == null)
                {
                    param.Add("STOCKLOCID", "0");
                }
                else
                {
                    param.Add("STOCKLOCID", StockLocId["Id"].ToString());
                }

                //辅助属性
                DynamicObject AUXPROPID = entryRow["AuxPropId"] as DynamicObject;
                if (AUXPROPID == null)
                {
                    param.Add("AUXPROPID", "0");
                }
                else
                {
                    param.Add("AUXPROPID", AUXPROPID["Id"].ToString());
                }

                //库存状态
                DynamicObject STOCKSTATUSID = entryRow["StockStatusId"] as DynamicObject;
                if (STOCKSTATUSID == null)
                {
                    param.Add("STOCKSTATUSID", "0");
                }
                else
                {
                    param.Add("STOCKSTATUSID", STOCKSTATUSID["Id"].ToString());
                }

                //批号
                //DynamicObject Lot = entryRow["Lot_Text"] as DynamicObject;
                if (entryRow["Lot_Text"] == null)
                {
                    param.Add("LOT", "");
                }
                else
                {
                    param.Add("LOT", entryRow["Lot_Text"].ToString());
                }

                //BOM版本
                DynamicObject BOMID = entryRow["BOMId"] as DynamicObject;
                if (BOMID == null)
                {
                    param.Add("BOMID", "0");
                }
                else
                {
                    param.Add("BOMID", BOMID["Id"].ToString());
                }

                //计划跟踪号
                DynamicObject MTONO = entryRow["MtoNo"] as DynamicObject;
                if (MTONO == null)
                {
                    param.Add("MTONO", "");
                }
                else
                {
                    param.Add("MTONO", MTONO["Id"].ToString());
                }

                //项目编号 FPROJECTNO
                DynamicObject PROJECTNO = entryRow["ProjectNo"] as DynamicObject;
                if (PROJECTNO == null)
                {
                    param.Add("PROJECTNO", "");
                }
                else
                {
                    param.Add("PROJECTNO", PROJECTNO["Id"].ToString());
                }

                //生产日期 FPRODUCEDATE
                DynamicObject PRODUCEDATE = entryRow["ProduceDate"] as DynamicObject;
                if (PRODUCEDATE == null)
                {
                    param.Add("PRODUCEDATE", "");
                }
                else
                {
                    param.Add("PRODUCEDATE", PRODUCEDATE["Id"].ToString());
                }

                //有效期至 FEXPIRYDATE
                DynamicObject EXPIRYDATE = entryRow["EXPIRYDATE"] as DynamicObject;
                if (EXPIRYDATE == null)
                {
                    param.Add("EXPIRYDATE", "");
                }
                else
                {
                    param.Add("EXPIRYDATE", EXPIRYDATE["Id"].ToString());
                }

                //物料内码
                DynamicObject MaterialId = entryRow["MaterialId"] as DynamicObject;
                if (MaterialId == null)
                {
                    param.Add("MATERIALID", "");
                }
                else
                {
                    param.Add("MATERIALID", MaterialId["Id"].ToString());
                }

                //长度
                //DynamicObject F_ST_QTY = entryRow["F_st_Qty"] as DynamicObject;
                DynamicObject F_ST_UnitID = entryRow["F_st_UnitID"] as DynamicObject;
                if (entryRow["F_st_Qty"] == null)
                {
                    param.Add("F_ST_QTY", "0");
                }
                else
                {
                    if (F_ST_UnitID != null)
                    {
                        strSql = string.Format(@"SELECT FCONVERTNUMERATOR,FCONVERTDENOMINATOR FROM T_BD_UNITCONVERTRATE WHERE FUNITID='{0}'", F_ST_UnitID["Id"].ToString());
                        DataTable Reader = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];

                        //Reader.GetData(0); 
                        double dblConvertNumerator = Convert.ToDouble(Reader.Rows[0]["FCONVERTNUMERATOR"].ToString());
                        double dblConvertDenminator = Convert.ToDouble(Reader.Rows[0]["FCONVERTDENOMINATOR"].ToString());
                        double dblLengthQty = Convert.ToDouble(entryRow["F_st_Qty"].ToString()) * dblConvertNumerator / dblConvertDenminator;
                        param.Add("F_ST_QTY", dblLengthQty.ToString());
                    }
                    else
                    {
                        param.Add("F_ST_QTY", entryRow["F_st_Qty"].ToString());
                    }
                }

                //支数
                //DynamicObject F_ST_COUNTQTY = entryRow["F_st_COUNTQTY"] as DynamicObject;
                if (entryRow["F_st_COUNTQTY"] == null)
                {
                    param.Add("F_ST_COUNTQTY", "0");
                }
                else
                {
                    param.Add("F_ST_COUNTQTY", entryRow["F_st_COUNTQTY"].ToString());
                }

                //双数
                //DynamicObject F_ST_PAIRQTY = entryRow["F_st_PAIRQTY"] as DynamicObject;
                if (entryRow["F_st_PAIRQTY"] == null)
                {
                    param.Add("F_ST_PAIRQTY", "0");
                }
                else
                {
                    param.Add("F_ST_PAIRQTY", entryRow["F_st_PAIRQTY"].ToString());
                }
                #endregion
                paramList.Add(param);
            }

            //将父单据体每行的数据信息传递
            paramL = paramList;

            //在单据保存前，创建临时表(中间表)，将从数据库后台读取当前单据的相关数量到中间表中(注意：这里数量需要*-1)
            /*
             * ********* 代码实现区 ***************
             */
            if (openType == OperationStatus.EDIT)
            {
                //在编辑时，单据状态不为“暂存”的情况下才进行库存更新
                if (this.View.Model.GetValue("FDocumentStatus").ToString() != "暂存")
                {
                    //获取当前单据相关信息
                    DynamicObject DataInfo = this.Model.DataObject as DynamicObject;
              
                    #region 保存前的数据
                    //明细信息
                    strSql = string.Format(@"SELECT u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        v.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        -1*SUM(u.F_st_Qty*t1.FCONVERTNUMERATOR/t1.FCONVERTDENOMINATOR) AS [F_ST_Qty],
                                        -1*SUM([F_ST_COUNTQTY]) AS [F_ST_COUNTQTY],-1*SUM([F_ST_PAIRQTY]) AS [F_ST_PAIRQTY]  
                                        FROM [dbo].[{0}] v INNER JOIN [dbo].[{1}] u ON u.FID=v.FID  
                                        LEFT JOIN T_BD_UNITCONVERTRATE t1 ON t1.FUNITID=u.F_st_UNITID  
                                        WHERE v.FID={2} GROUP BY u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        v.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID]"
                        , strHeadTable, strEntryTable, DataInfo["Id"].ToString());
                    oldData = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];

                    //双数明细
                    strSql = string.Format(@"SELECT u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        v.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        [F_ST_COLOR],[F_ST_LR],[F_ST_Size],-1*SUM([F_ST_PairDetailQty]) AS [F_ST_PairDetailQty] 
                                        FROM [dbo].[{0}] v INNER JOIN [dbo].[{1}] u ON u.FID=v.FID 
                                        INNER JOIN [dbo].[{2}] w ON w.FentryID=u.FEntryID 
                                        LEFT JOIN T_BD_UNITCONVERTRATE t1 ON t1.FUNITID=u.F_st_UNITID  
                                        WHERE v.FID={3} GROUP BY u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        v.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        [F_ST_COLOR],[F_ST_LR],[F_ST_Size]"
                        , strHeadTable, strEntryTable, strPairDetailTable, DataInfo["Id"].ToString());
                    oldDataDetail = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];
                    #endregion

                    #region 
                    for (int i = 1; i < paramList.Count; i++)
                    {
                        //当前单据明细信息
                        strSql = string.Format(@"SELECT u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        u.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        SUM(u.[F_ST_Qty]) AS [F_ST_Qty],SUM(u.[F_ST_COUNTQTY]) AS [F_ST_COUNTQTY],SUM(u.[F_ST_PAIRQTY]) AS [F_ST_PAIRQTY] 
                                        FROM (SELECT {0} AS [FOWNERTYPEID],{1} AS [FOWNERID],{2} AS [FKEEPERTYPEID],{3} AS [FKEEPERID],
                                        {4} AS [FSTOCKORGID],{5} AS [FSTOCKID],{6} AS [FSTOCKLOCID],{7} AS [FAUXPROPID],{8} AS [FSTOCKSTATUSID],
                                        {9} AS [FLOT_TEXT],{10} AS [FBOMID],{11} AS [FMTONO],{12} AS [FPROJECTNO],{13} AS [FPRODUCEDATE],
                                        {14} AS [FEXPIRYDATE],{15} AS [FMATERIALID],{16} AS [F_ST_Qty],{17} AS [F_ST_COUNTQTY],{18} AS [F_ST_PAIRQTY]) u  
                                        GROUP BY u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        v.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID]"
                            ,paramList[i]["OWNERTYPEID"],paramList[i]["OWNERID"],paramList[i]["KEEPERTYPEID"],paramList[i]["KEEPERID"]
                            ,paramList[i]["STOCKORGID"],paramList[i]["STOCKID"],paramList[i]["STOCKLOCID"],paramList[i]["AUXPROPID"],paramList[i]["STOCKSTATUSID"]
                            ,paramList[i]["LOT"],paramList[i]["BOMID"],paramList[i]["MTONO"],paramList[i]["PROJECTNO"],paramList[i]["PRODUCEDATE"]
                            ,paramList[i]["EXPIRYDATE"],paramList[i]["MATERIALID"],paramList[i]["F_ST_QTY"],paramList[i]["F_ST_COUNTQTY"],paramList[i]["F_ST_PAIRQTY"]);
                        newData =  DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0]; 

                        //即时库存明细信息
                        strSql = string.Format(@"SELECT v.[FOWNERTYPEID],v.[FOWNERID],v.[FKEEPERTYPEID],v.[FKEEPERID],
                                        v.[FSTOCKORGID],v.[FSTOCKID],v.[FSTOCKLOCID],v.[FAUXPROPID],v.[FSTOCKSTATUSID],
                                        x.[FLOT_TEXT],v.[FBOMID],v.[FMTONO],v.[FPROJECTNO],v.[FPRODUCEDATE],v.[FEXPIRYDATE],v.[FMATERIALID],
                                        u.[F_ST_Qty],u.[F_ST_COUNTQTY],u.[F_ST_PAIRQTY]  
                                        FROM T_STK_INVENTORY v INNER JOIN st_T_STK_INVENTORY_EXP u ON u.FID=v.FID LEFT JOIN T_BD_LOTMSATER x ON x.FLotID=v.FLotID 
                                        WHERE v.[FOWNERTYPEID]='{0}',v.[FOWNERID]='{1}',v.[FKEEPERTYPEID]='{2}',v.[FKEEPERID]='{3}',
                                        v.[FSTOCKORGID]='{4}',v.[FSTOCKID]='{5}',v.[FSTOCKLOCID]='{6}',v.[FAUXPROPID]='{7}',v.[FSTOCKSTATUSID]='{8}',
                                        v.[FLOT_TEXT]='{9}',v.[FBOMID]='{10}',v.FMTONO]='{11}',v.[FPROJECTNO]='{12}',v.[FPRODUCEDATE]='{13}',
                                        v.[FEXPIRYDATE]='{14}',v.[FMATERIALID]='{15}'"
                            , paramList[i]["OWNERTYPEID"], paramList[i]["OWNERID"], paramList[i]["KEEPERTYPEID"], paramList[i]["KEEPERID"]
                            , paramList[i]["STOCKORGID"], paramList[i]["STOCKID"], paramList[i]["STOCKLOCID"], paramList[i]["AUXPROPID"], paramList[i]["STOCKSTATUSID"]
                            , paramList[i]["LOT"], paramList[i]["BOMID"], paramList[i]["MTONO"], paramList[i]["PROJECTNO"], paramList[i]["PRODUCEDATE"]
                            , paramList[i]["EXPIRYDATE"], paramList[i]["MATERIALID"]);
                        invData = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];
                    }

                    //双数明细
                    for (int i = 1; i < pairDetailList.Count; i++)
                    {
                        strSql = string.Format(@"SELECT u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        u.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        u.[F_ST_COLOR],u.[F_ST_LR],u.[F_ST_SIZE],SUM(u.[F_ST_PAIRDETAILQTY]) AS [F_ST_PairDetailQty]  
                                        FROM (SELECT {0} AS [FOWNERTYPEID],{1} AS [FOWNERID],{2} AS [FKEEPERTYPEID],{3} AS [FKEEPERID],
                                        {4} AS [FSTOCKORGID],{5} AS [FSTOCKID],{6} AS [FSTOCKLOCID],{7} AS [FAUXPROPID],{8} AS [FSTOCKSTATUSID],
                                        {9} AS [FLOT_TEXT],{10} AS [FBOMID],{11} AS [FMTONO],{12} AS [FPROJECTNO],{13} AS [FPRODUCEDATE],
                                        {14} AS [FEXPIRYDATE],{15} AS [FMATERIALID],{16} AS [F_ST_COLOR],{17} AS [F_ST_LR],{18} AS [F_ST_SIZE],{19} AS [F_ST_PAIRDETAILQTY]) u 
                                        GROUP BY u.[FOWNERTYPEID],u.[FOWNERID],u.[FKEEPERTYPEID],u.[FKEEPERID],
                                        u.[FSTOCKORGID],u.[FSTOCKID],u.[FSTOCKLOCID],u.[FAUXPROPID],u.[FSTOCKSTATUSID],
                                        u.[FLOT_TEXT],u.[FBOMID],u.[FMTONO],u.[FPROJECTNO],u.[FPRODUCEDATE],u.[FEXPIRYDATE],u.[FMATERIALID],
                                        u.[F_ST_COLOR],u.[F_ST_LR],u.[F_ST_SIZE]"
                            , pairDetailList[i]["OWNERTYPEID"],pairDetailList[i]["OWNERID"],pairDetailList[i]["KEEPERTYPEID"],pairDetailList[i]["KEEPERID"]
                            , pairDetailList[i]["STOCKORGID"],pairDetailList[i]["STOCKID"],pairDetailList[i]["STOCKLOCID"],pairDetailList[i]["AUXPROPID"],pairDetailList[i]["STOCKSTATUSID"]
                            , pairDetailList[i]["LOT"],pairDetailList[i]["BOMID"],pairDetailList[i]["MTONO"],pairDetailList[i]["PROJECTNO"],pairDetailList[i]["PRODUCEDATE"]
                            , pairDetailList[i]["EXPIRYDATE"], pairDetailList[i]["MATERIALID"], pairDetailList[i]["F_ST_COLOR"], pairDetailList[i]["F_ST_LR"], pairDetailList[i]["F_ST_SIZE"], pairDetailList[i]["F_ST_PAIRDETAILQTY"]);
                        newDataDetail =  DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];

                        //即时库存双数明细
                        strSql = string.Format(@"SELECT v.[FOWNERTYPEID],v.[FOWNERID],v.[FKEEPERTYPEID],v.[FKEEPERID],
                                        v.[FSTOCKORGID],v.[FSTOCKID],v.[FSTOCKLOCID],v.[FAUXPROPID],v.[FSTOCKSTATUSID],
                                        x.[FLOT_TEXT],v.[FBOMID],v.[FMTONO],v.[FPROJECTNO],v.[FPRODUCEDATE],v.[FEXPIRYDATE],v.[FMATERIALID],
                                        w.[F_ST_COLOR],w.[F_ST_LR],w.[F_ST_SIZE],w.[F_ST_PAIRDETAILQTY] 
                                        FROM T_STK_INVENTORY v INNER JOIN st_T_STK_INVENTORY_EXP u ON u.FID=v.FID INNER JOIN st_T_STK_INVENTORY_EXPENTRY w ON w.FID=u.FID LEFT JOIN T_BD_LOTMSATER x ON x.FLotID=v.FLotID 
                                        WHERE v.[FOWNERTYPEID]='{0}',v.[FOWNERID]='{1}',v.[FKEEPERTYPEID]='{2}',v.[FKEEPERID]='{3}',
                                        v.[FSTOCKORGID]='{4}',v.[FSTOCKID]='{5}',v.[FSTOCKLOCID]='{6}',v.[FAUXPROPID]='{7}',v.[FSTOCKSTATUSID]='{8}',
                                        v.[FLOT_TEXT]='{9}',v.[FBOMID]='{10}',v.FMTONO]='{11}',v.[FPROJECTNO]='{12}',v.[FPRODUCEDATE]='{13}',
                                        v.[FEXPIRYDATE]='{14}',v.[FMATERIALID]='{15}',x.[F_ST_COLOR]={16},x.[F_ST_LR]={17},x.[F_ST_SIZE]={18}"
                            , pairDetailList[i]["OWNERTYPEID"], pairDetailList[i]["OWNERID"], pairDetailList[i]["KEEPERTYPEID"], pairDetailList[i]["KEEPERID"]
                            , pairDetailList[i]["STOCKORGID"], pairDetailList[i]["STOCKID"], pairDetailList[i]["STOCKLOCID"], pairDetailList[i]["AUXPROPID"], pairDetailList[i]["STOCKSTATUSID"]
                            , pairDetailList[i]["LOT"], pairDetailList[i]["BOMID"], pairDetailList[i]["MTONO"], pairDetailList[i]["PROJECTNO"], pairDetailList[i]["PRODUCEDATE"]
                            , pairDetailList[i]["EXPIRYDATE"], pairDetailList[i]["MATERIALID"], pairDetailList[i]["F_ST_COLOR"], pairDetailList[i]["F_ST_LR"], pairDetailList[i]["F_ST_SIZE"], pairDetailList[i]["F_ST_PAIRDETAILQTY"]);
                        invDataDetail = DBUtils.ExecuteDataSet(this.Context, strSql).Tables[0];
                    }
                    #endregion

                }
                
            }
        }
        #endregion
    }
}
